<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Cache;
use addons\epay\library\Service;

class My extends Frontend {

    public function index() {

        $user_info = db('users')->where(['user_id' => $this->iUserId])->find();
        if ($user_info) {
            if (strpos($user_info['avatar'], 'http') === false) {
                $user_info['avatar'] = 'http://mohebianli.oss-cn-zhangjiakou.aliyuncs.com' . $user_info['avatar'];
            }
        }
        $wallet = db('wallet')->where(['user_id' => $this->iUserId])->find();
        if (empty($wallet)) {
            $wallet['balance'] = 0.00;
        }

        $coupon_num = db('user_coupon')->where(['user_id' => $this->iUserId, 'status' => 0])->count();


        $this->assign('wallet', $wallet);
        $this->assign('coupon_num', $coupon_num);
        $this->assign('user_info', $user_info);
        return $this->view->fetch();
    }

    public function info() {
        $user_info = db('users')->where(['user_id' => $this->iUserId])->find();
        if ($user_info) {
            if (strpos($user_info['avatar'], 'http') === false) {
                $user_info['avatar'] = 'http://mohebianli.oss-cn-zhangjiakou.aliyuncs.com' . $user_info['avatar'];
            }

            $user_info['hide_phone'] = substr_replace($user_info['phone'], '****', 3, 4);
        }
        $this->assign('user_info', $user_info);
        return $this->view->fetch();
    }

    public function setBir() {
        $bir = $this->request->param('bir');
        $data['birthday'] = $bir;
        $res = db('users')->where(['user_id' => $this->iUserId])->update($data);
    }

    public function setNickname() {
        if ($this->request->isPost()) {
            $nick_name = $this->request->param('nick_name');

            if (empty($nick_name)) {
                $this->error('请输入新昵称');
            }

            $data['nick_name'] = $nick_name;
            $res = db('users')->where(['user_id' => $this->iUserId])->update($data);

            if ($res) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        } else {
            return $this->view->fetch();
        }
    }

    public function setPhoneOne() {
        if ($this->request->isPost()) {
            $phone = $this->request->param('phone');
            $vcode = $this->request->param('vcode');

            if (Cache::get($phone) != $vcode) {
                $this->error('验证码错误');
            }

            //删除缓存
            Cache::rm($phone);

            $this->success('验证成功');
        } else {
            $user_info = db('users')->where(['user_id' => $this->iUserId])->find();

            if ($user_info) {
                $user_info['hide_phone'] = substr_replace($user_info['phone'], '****', 3, 4);
            }
            $this->assign('user_info', $user_info);
            return $this->view->fetch();
        }
    }

    public function setPhoneTwo() {
        if ($this->request->isPost()) {
            $phone = $this->request->param('phone');
            $vcode = $this->request->param('vcode');


            if (empty($phone)) {
                $this->error('请填写手机号');
            }
            if (Cache::get($phone) != $vcode) {
                $this->error('验证码错误');
            }

            //删除缓存
            Cache::rm($phone);

            $data['phone'] = $phone;
            $res = db('users')->where(['user_id' => $this->iUserId])->update($data);
            $this->success('修改成功');
        } else {
            return $this->view->fetch();
        }
    }

    public function setAvatar() {
        $avatar = $this->request->param('avatar');

        $data['avatar'] = $avatar;
        $res = db('users')->where(['user_id' => $this->iUserId])->update($data);
        $this->success('修改成功');
    }

    public function recharge() {

        $list = db('recharge')->order('price asc')->select();

        $this->assign('list', $list);
        return $this->view->fetch();
    }

    /**
     * 微信充值
     */
    public function wxRecharge() {
        $recharge_id = $this->request->param('rid', 0);
        if ($recharge_id == 0) {
            $this->error('请选择要充值的金额');
        }

        $info = db('recharge')->where('recharge_id', '=', $recharge_id)->find();

        if (empty($info)) {
            $this->error('错误的充值金额');
        }

        $recharge_price = $info['price'];

        $user_id = $this->iUserId;
        $orderData['user_id'] = $user_id;
        $orderData['order_no'] = 'CZ' . time() . mt_rand(1000, 9999);
        $orderData['type'] = 10;   //商品类型
        $orderData['pay_type'] = 1;
        $orderData['goods_price'] = $recharge_price;
        $orderData['real_price'] = $recharge_price;
        $orderData['status'] = 0;

        //获取赠送金额
        $rwhere['price'] = array('elt', $recharge_price);
        $rwhere['is_disabled'] = 0;
        $recharge = Db::name('recharge')->where($rwhere)->order('price desc')->find();

        if ($recharge) {
            $orderData['give_price'] = $recharge['give'];
        }

        $res = db('order')->insert($orderData);
        if ($res) {

            $url = url('Wxpay/pay', array('out_trade_no' => $orderData['order_no']));
            $this->success('成功', ['url' => $url]);
            // 前往支付
//            redirect($url);
        } else {
            $this->error('充值失败');
        }
    }

    public function wallet() {
        $type = $this->request->param('type', 0); //0收入 1支出
        $wallet = db('wallet')->where(['user_id' => $this->iUserId])->find();
        if (empty($wallet)) {
            $wallet['balance'] = 0.00;
        }

        $wallet_log = db('wallet_log')
                ->where(['wallet_id' => $wallet['wallet_id'], 'change_type' => $type, 'change_amount' => ['neq', 0]])
                ->limit(10)
                ->order('createtime desc')
                ->select();


        if ($wallet_log) {
            foreach ($wallet_log as &$val) {
                if ($val['order_id']) {
                    $val['order_info'] = db('order')->where(['order_id' => $val['order_id']])->find();
                }
            }
            unset($val);
        } else {
            $wallet_log = [];
        }

        $this->assign('type', $type);
        $this->assign('list', $wallet_log);
        $this->assign('wallet', $wallet);
        return $this->view->fetch();
    }

    public function walletItem() {
        $type = $this->request->param('type', 0);
        $p = $this->request->param('p', 0);
        $size = $this->request->param('psize', 6);
        $page = 0;
        if ($p) {
            $page = $p * $size;
        }

        $wallet = db('wallet')->where(['user_id' => $this->iUserId])->find();

        $wallet_log = db('wallet_log')
                ->where(['wallet_id' => $wallet['wallet_id'], 'change_type' => $type, 'change_amount' => ['neq', 0]])
                ->limit("$page,$size")
                ->order('createtime desc')
                ->select();

        if ($wallet_log) {
            foreach ($wallet_log as &$val) {
                if ($val['order_id']) {
                    $val['order_info'] = db('order')->where(['order_id' => $val['order_id']])->find();
                }
            }
            unset($val);
        } else {
            $wallet_log = [];
        }


        $this->assign('list', $wallet_log);
        return $this->view->fetch();
    }

    public function score() {
        $type = $this->request->param('type', 0); //0收入 1支出
        $user_info = db('users')->where(['user_id' => $this->iUserId])->find();

        $score_log = db('score_log')
                ->where(['user_id' => $this->iUserId, 'change_type' => $type])
                ->limit(10)
                ->order('createtime desc')
                ->select();


        if ($score_log) {
            foreach ($score_log as &$val) {
                if ($val['order_id']) {
                    $val['order_info'] = db('order')->where(['order_id' => $val['order_id']])->find();
                }
            }
            unset($val);
        } else {
            $score_log = [];
        }

        $this->assign('type', $type);
        $this->assign('list', $score_log);
        $this->assign('user_info', $user_info);
        return $this->view->fetch();
    }

    public function scoreItem() {
        $type = $this->request->param('type', 0);
        $p = $this->request->param('p', 0);
        $size = $this->request->param('psize', 6);
        $page = 0;
        if ($p) {
            $page = $p * $size;
        }

        $score_log = db('score_log')
                ->where(['user_id' => $this->iUserId, 'change_type' => $type])
                ->limit("$page,$size")
                ->order('createtime desc')
                ->select();


        if ($score_log) {
            foreach ($score_log as &$val) {
                if ($val['order_id']) {
                    $val['order_info'] = db('order')->where(['order_id' => $val['order_id']])->find();
                }
            }
            unset($val);
        } else {
            $score_log = [];
        }

        $this->assign('list', $score_log);
        return $this->view->fetch();
    }

    public function opinion() {
        if ($this->request->isPost()) {
            $content = $this->request->param('content');

            $data['user_id'] = $this->iUserId;
            $data['content'] = $content;

            db('opinion')->insert($data);

            $this->redirect(url('opinionList'));
        } else {
            return $this->view->fetch();
        }
    }

    public function opinionList() {
        $list = db('opinion')->where(['user_id' => $this->iUserId])->order('createtime desc')->select();

        $this->assign('list', $list);
        return $this->view->fetch();
    }

    public function coupon() {
        $status = input('s', 0);
        $list = db('user_coupon')
                ->alias('uc')
                ->field('uc.*,c.coupon_name,c.par_value,c.use_condition,c.range,c.relation_id')
                ->join('fa_coupon c', 'uc.coupon_id = c.coupon_id')
                ->where('uc.status', $status)
                ->where('user_id', $this->iUserId)
                ->limit(10)
                ->order('user_coupon_id desc')
                ->select();

        if ($list) {
            foreach ($list as &$val) {
                $val['expiry_end'] = date('Y-m-d', strtotime($val['expiry_end']));
                $val['usetime'] = date('Y-m-d', strtotime($val['usetime']));

                $val['name'] = '全场通用';

                if ($val['range'] == 1) {
                    $brand_title = db('goods_brand')->where('brand_id', 'in', $val['relation_id'])->column('brand_title');
                    $val['name'] = '适用于' . implode(',', $brand_title) . '品牌商品';
                }

                if ($val['range'] == 2) {
                    $cate_title = db('goods_cate')->where('cate_id', 'in', $val['relation_id'])->column('cate_title');
                    $val['name'] = '适用于' . implode(',', $cate_title) . '品类商品';
                }
            }
        }

        $this->assign('list', $list);
        $this->assign('status', $status);
        return $this->view->fetch();
    }

    public function couponItem() {
        $status = $this->request->param('s', 0);
        $p = $this->request->param('p', 0);
        $size = $this->request->param('psize', 10);
        $page = 0;
        if ($p) {
            $page = $p * $size;
        }

        $list = db('user_coupon')
                ->alias('uc')
                ->field('uc.*,c.coupon_name,c.par_value,c.use_condition,c.range,c.relation_id')
                ->join('fa_coupon c', 'uc.coupon_id = c.coupon_id')
                ->where('uc.status', $status)
                ->where('user_id', $this->iUserId)
                ->order('uc.user_coupon_id desc')
                ->limit("$page,$size")
                ->select();

        if ($list) {
            foreach ($list as &$val) {
                $val['expiry_end'] = date('Y-m-d', strtotime($val['expiry_end']));
                $val['usetime'] = date('Y-m-d', strtotime($val['usetime']));

                $val['name'] = '全场通用';

                if ($val['range'] == 1) {
                    $brand_title = db('goods_brand')->where('brand_id', 'in', $val['relation_id'])->column('brand_title');
                    $val['name'] = '适用于' . implode(',', $brand_title) . '品牌商品';
                }

                if ($val['range'] == 2) {
                    $cate_title = db('goods_cate')->where('cate_id', 'in', $val['relation_id'])->column('cate_title');
                    $val['name'] = '适用于' . implode(',', $cate_title) . '品类商品';
                }
            }
        }

        $this->assign('list', $list);
        return $this->view->fetch();
    }

}
